package red.util;


import java.util.List;

public class PageHelper {

    private List<?> list;//对象记录结果集
    private int total;//总条数
    private int pageSize;//每页显示记录数
    private int page;//当前页
    private int pages;//总页数
    private boolean hasPreviousPage=false;  //是否有前一页
    private boolean hasNextPage=false;  //是否有下一页

    public PageHelper(int total, String page, String pageSize, List list) {
        super();
        if (page.length()==0&&pageSize.length()==0){
            this.page = Integer.parseInt(String.valueOf(1));
        }else {
            this.page = Integer.parseInt(page);
        }
        if (page.length()==0&&pageSize.length()==0){
            this.pageSize = total;
        }else {
            this.pageSize = Integer.parseInt(pageSize);
        }
        this.total = total;
        this.list = list;
    }

    //对象记录结果集
    public List<?> getList() {
        return list;
    }
    public void setList(List<?> list) {
        this.list = list;
    }

    //总条数
    public int getTotal() {
        return total;
    }
    public void setTotal(int total) {
        this.total = total;
    }

    //每页显示记录数
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    //当前页
    public int getPage() {
        return page;
    }
    public void setPage(int page) {
        this.page = page;
    }

    //总页数
    public int getPages() {
        if (0==total%pageSize){
            pages = total/pageSize;
        }else {
            pages = total/pageSize+1;
        }
        if (0==pages)
        {
            pages = 1;
        }
        return pages;
    }

    //是否有上一页
    public boolean hasPreviousPage() {
        if (page==1){
            return hasPreviousPage = false;
        }else {
            return hasPreviousPage = true;
        }
    }

    //是否有下一页
    public boolean hasNextPage() {
        if (page==pages){
            return hasNextPage = false;
        }else {
            if (page>pages){
                return hasNextPage = false;
            }else {
                return hasNextPage = true;
            }
        }
    }
}
